<?php
/*
通用分类
*/
class Con_cg extends Acan{
	public $defaultAct='index';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){
			$this->id = '1';
		}
		if($this->aurl_a_name=='index' || empty($this->aurl_a_name)){//默认系统分类
			$_SESSION['app']['cg']['dbname'] = Acan_table_cg;
		}elseif($this->aurl_a_name=='user'){//用户分类
			if($_GET['uid']){$_SESSION['app']['cg']['uid'] = $_GET['uid'];}
			$_SESSION['app']['cg']['dbname'] = Acan_table_cg.'_'.$this->aurl_a_name;
		}
		if($_SESSION['app']['cg']['dbname'] == Acan_table_cg.'_user'){
			if($_SESSION['app']['cg']['uid']){
				$this->uid = $_SESSION['app']['cg']['uid'];
			}else{
				Afun_refresh('请先选择用户',$this->aurl_i.'member');
			}
		}
		$this->cg_dbname = $_SESSION['app']['cg']['dbname'];
		$this->cg = Afun_db()->model($this->cg_dbname);
	}
	function Act_user(){
		$this->uinfo = Afun_new('member')->info($this->uid);//会员信息
		$this->title = '用户';
		return $this->Act_index();
	}
	function Act_index(){
		if(empty($this->title)){$title = '系统';}
		$form_val_is_show = array('1'=>'是','2'=>'否');
		//分页函数
		$con->where = "`pid`='$this->id'";
		if($this->uid){
			$con->where .= " and `uid`='".$this->uid."'";
		}
		$page->theurl = $this->aurl_a.'/'.$this->id;
		$page->by = $this->cg->key_name;
		$page->page = '20';
		$page->counts = $this->cg->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.if_use asc,a.look asc,a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$result = $this->cg->findAll($con);
		foreach ($result as $key => $val) {
			if($_GET['search']){
				$val['name']=preg_replace("/($key)/i","<font color=red><b>\\1</b></font>",$val['name']);
			}
			if($val['cg']==2){
				$val['urllink']="index.php?".$val['url'];
			}else if($row['cg']==3){
				$val['urllink']="app.php?a=".$val['url'];
			}
			$list[] = $val;
		}
		//获取父菜单信息
		$listp= $this->cg->findPk($this->id);
		include $this->Atpl($this->Amod.'/index');
	}
	//批量更新
	function Act_save(){
		$this->del_cache();
		foreach($_POST['save'] as $i=>$vals){
			$this->cg->updatePk($vals,$vals['id']);
		}
		Afun_refresh('保存成功','referrer');
	}
	//添加
	function Act_add(){
		$this->del_cache();
		$_POST['add']['pid'] = $this->id;
		if($this->uid){$_POST['add']['uid'] = $this->uid;}
		if($this->cg->save($_POST['add']))
			Afun_refresh('添加成功','referrer');
		else
			Afun_refresh('添加失败','referrer');
	}
	//删除
	function Act_del(){
		$this->del_cache();
		if($this->cg->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//删除所选
	function Act_delall(){
		$this->del_cache();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->cg->delete($this->cg->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	function del_cache(){
		Afun_cache_rmAll($this->cg_dbname.'/');//有数据库操作删除缓存
	}
	
}

?>